package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.GvServiceAdapter;
import com.yanq.liancar.adapter.GvServiceImgAdapter;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.FullServiceBean;
import com.yanq.liancar.bean.ServiceTypeBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmallBServiceDescriptionActivity extends BaseToolbarActivity {

    @BindView(R.id.gridview_service)
    GridView gridviewService;
    @BindView(R.id.et_servicename)
    EditText etServicename;
    @BindView(R.id.et_serviceprice)
    EditText etServiceprice;
    @BindView(R.id.et_serviceblurb)
    EditText etServiceblurb;
    @BindView(R.id.et_servicematter)
    EditText etServicematter;
    @BindView(R.id.img_gridview)
    GridView imgGridview;
    @BindView(R.id.tv_AddService)
    TextView addService;

    private GvServiceAdapter gvServiceAdapter;
    private GvServiceImgAdapter gvServiceImgAdapter;
    private List<ServiceTypeBean> serviceList = new ArrayList<>();
    private List<Integer> imglist;
    private String strType;
    private int intType;
    private List<FullServiceBean.DataBean> mList = new ArrayList<>();
    private List<File> fileList=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_servicedescription);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("添加服务");
//        strType = getIntent().getStringExtra("stringType");
//        intType = getIntent().getIntExtra("intType", 0);

        addServiceType();
        addImg();
        //查询全部服务
        initOkGoService();
    }

    private void addServiceType() {
        gvServiceAdapter = new GvServiceAdapter(mContext, serviceList);
        gridviewService.setAdapter(gvServiceAdapter);
        gridviewService.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == parent.getChildCount() - 1) {
                    finish();
                } else {
                    for (int i = 0; i < serviceList.size(); i++) {
                        if (i == position) {
                            serviceList.get(i).setChecked(true);
                        } else {
                            serviceList.get(i).setChecked(false);
                        }
                    }
                    gvServiceAdapter.setData(serviceList);
                }
            }
        });
    }

    private void addImg() {
        imglist = new ArrayList<>();
        imglist.add(R.mipmap.license_bg);
        imglist.add(R.mipmap.license_bg);
        gvServiceImgAdapter = new GvServiceImgAdapter(mContext, fileList);
        imgGridview.setAdapter(gvServiceImgAdapter);
        imgGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (imglist.size() < 4) {
                    if (position == parent.getChildCount() - 1) {
                        imglist.add(R.mipmap.license_bg);
                        gvServiceImgAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }


    //全部服务
    private void initOkGoService() {
        OkGo.<String>post(Config.s + "llc/showCarService")
                .tag(this)
                .params("token", UserBean.getbToken(mContext))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FullServiceBean bean = gson.fromJson(response.body(), FullServiceBean.class);
                        if (bean.isIsSuccess()) {
                            mList.addAll(bean.getData());
                            for (int i = 0; i < mList.size(); i++) {
                                ServiceTypeBean serviceTypeBean = new ServiceTypeBean();
                                serviceTypeBean.setName(mList.get(i).getServiceName());
                                serviceTypeBean.setChecked(false);
                                serviceTypeBean.setId(mList.get(i).getId());
                                serviceTypeBean.setServiceType(mList.get(i).getServiceType());
                                serviceList.add(serviceTypeBean);
                            }
                            ServiceTypeBean typeBean=new ServiceTypeBean();
                            typeBean.setServiceType(intType);
                            typeBean.setChecked(true);
                            typeBean.setName(strType);
                            serviceList.add(typeBean);
                            LogUtils.e("1111111", serviceList.size() + "");
                            gridviewService.setAdapter(gvServiceAdapter);
                            //gvServiceAdapter.setData(serviceList);
                        } else {
                            RxToast.success(bean.getOutputMessage());
                        }
                    }
                });

    }

    //添加车辆服务
    private void initOkGoAddService() {
        OkGo.<String>post(Config.s + "llc/addCarService")
                .tag(this)
                .params("token", UserBean.getbToken(mContext))
                .params("serviceType", intType)
                .params("serviceName", strType)
                .params("servicePrice",etServiceprice.getText().toString().trim() )
                .params("serviceInfo", etServiceblurb.getText().toString().trim())
                .params("serviceRemark",etServicematter.getText().toString().trim() )
                .addFileParams("file",fileList)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }
                });

    }
}
